package alg20241121;

public class Text2 {
    public int calculateMinimumHP(int[][] dungeon) {
        int m = dungeon.length;
        int n = dungeon[0].length;
        //1 创建表
        int[][] dp = new int[m+1][n+1];
        //2 初始化
        for (int i = 0; i <= m; i++) {
            dp[i][n] = Integer.MAX_VALUE;
        }
        for (int i = 0; i <= n; i++) {
            dp[m][i] = Integer.MAX_VALUE;
        }
        dp[m-1][n] = 1;
        //3 填表
        for (int i = m-1; i >= 0; i--) {
            for (int j = n-1; j >= 0; j--) {
                dp[i][j] = Math.min(dp[i][j+1], dp[i+1][j]) - dungeon[i][j];
                //判断血包是否过大
                dp[i][j] = Math.max(1, dp[i][j]);
            }
        }
        //4 返回值
        return dp[0][0];

    }
}
